﻿using System;
using System.Data;
using System.Drawing;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class OfficerPage : System.Web.UI.Page
    {
        Officer aOfficer = new Officer();
        OfficerManager officerManager = new OfficerManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack != true)
            {
                PopulatedOfficerDesignationDropdownList();
            }
        }
        private void PopulatedOfficerDesignationDropdownList()
        {
            DesignationManager designationManager = new DesignationManager();
            DataTable designations = designationManager.GetAllOfficerDesignations();
            designationDropDownList.DataTextField = "Name";
            designationDropDownList.DataValueField = "Id";
            designationDropDownList.DataSource = designations;
            designationDropDownList.DataBind();
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            if (nameTextBox.Text != "" && permanentAddressTextBox.Text != "" && presentAddressTextBox.Text != "" && contactNumberTextBox.Text != "" && emailTextBox.Text != "")
            {
                try
                {
                    DateTime dateTime = DateTime.Today;
                    if (dateOfBirthTextBox.Text == "" && joiningDateTextBox.Text == "")
                    {
                        dateOfBirthTextBox.Text = dateTime.ToString();
                        joiningDateTextBox.Text = dateTime.ToString();
                    }
                    aOfficer.Name = nameTextBox.Text;
                    aOfficer.Designation.OfficerDesignation = Convert.ToInt32(designationDropDownList.SelectedValue);
                    aOfficer.FatherName = fatherNameTextBox.Text;
                    aOfficer.MotherName = motherNameTextBox.Text;
                    aOfficer.PresentAaddres = presentAddressTextBox.Text;
                    aOfficer.PermanentAaddres = permanentAddressTextBox.Text;
                    aOfficer.NationalIdNumber = nationalIdNumberTextBox.Text;
                    aOfficer.Email = emailTextBox.Text;
                    aOfficer.ContactNumber = contactNumberTextBox.Text;
                    aOfficer.DateOfBirth = Convert.ToDateTime(dateOfBirthTextBox.Text);
                    aOfficer.JoiningDate =Convert.ToDateTime( joiningDateTextBox.Text);
                   
                    int id = officerManager.GetPreviousOfficerId();
                    int currentId = id + 1;
                    aOfficer.FileName = imageFileUpload.FileName;
                    if (aOfficer.FileName == "")
                    {
                        saveMessageLabel1.Text = "Invalid filename supplied";
                    }
                    if (imageFileUpload.PostedFile.ContentLength == 0)
                    {
                        saveMessageLabel1.Text = "Invalid file content";
                    }
                    aOfficer.FileName =
                        System.IO.Path.GetFileName(aOfficer.FileName);
                    if (aOfficer.FileName == "")
                    {
                        saveMessageLabel1.Text = "Path not found";
                    }
                    try
                    {
                        if (imageFileUpload.HasFile)
                        {
                            string fileString = System.IO.Path.GetExtension(imageFileUpload.FileName);
                            if (fileString.ToLower() == ".jpg" || fileString.ToLower() == ".bmp" || fileString.ToLower() == ".gif")
                            {
                                imageFileUpload.SaveAs(@"F:\Update  Web Project\Image\" + currentId + imageFileUpload.FileName);
                                temporaryLocationLabel.Text = @"F:\Update  Web Project\Image\" + currentId + imageFileUpload.FileName;
                                saveMessageLabel1.ForeColor = Color.DeepPink;
                                saveMessageLabel1.Text = "File: " + imageFileUpload.PostedFile.FileName +
                                                              " has been uploaded.";
                            }
                            else
                            {
                                saveMessageLabel1.Text = "Only image file can be uploaded. Try again please.";
                            }
                        }
                    }
                    catch (Exception exObj)
                    {
                        saveMessageLabel1.Text = "Error: " + exObj.Message;
                    }
                    aOfficer.PastExperience = pastExperienceTextBox.Text;
                    aOfficer.FileLocation = temporaryLocationLabel.Text;
                    if (officerManager.SaveOfficer(aOfficer))
                    {
                        saveMessageLabel1.ForeColor = Color.Green;
                        saveMessageLabel1.Text = "Officer information Saved successfully.";
                    }
                }
                catch (Exception exception)
                {
                    saveMessageLabel1.ForeColor = Color.Red;
                    saveMessageLabel1.Text = exception.Message;
                }
            }
            else
            {
                saveMessageLabel1.ForeColor = Color.Red;
                saveMessageLabel1.Text = "Please fill up the mendatory fields";
            }
        }
    }
}